package cn.gyouzhe.mapper;

import cn.gyouzhe.entity.Good;
import cn.gyouzhe.mapper.provider.GoodProvider;
import cn.gyouzhe.mapper.vo.ColumnCountVo;
import cn.gyouzhe.vo.GoodVo;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品mapper
 *
 * @author wangchuan
 * @datetime 2018/2/28 23:16
 */
@Mapper
public interface GoodMapper {

    /**
     * 根据商品ID查询商品
     * @param goodId 商品ID
     * @return 商品实体
     */
    @Select("select * from g_good where good_id=#{goodId}")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "goodId", column = "good_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "shortTitle", column = "short_title"),
            @Result(property = "desc", column = "introduce"),
            @Result(property = "pic", column = "pic"),
            @Result(property = "price", column = "price"),
            @Result(property = "commission", column = "commission"),
            @Result(property = "cid", column = "cid"),
            @Result(property = "tmall", column = "tmall"),
            @Result(property = "sellNum", column = "sell_num"),
            @Result(property = "sellerId", column = "seller_id"),
            @Result(property = "couponId", column = "coupon_id"),
            @Result(property = "del", column = "del"),
            @Result(property = "createTime", column = "create_at"),
            @Result(property = "updateTime", column = "update_at")
    })
    Good selectGood(@Param("goodId") String goodId);

    /**
     * 查询商品栏目总数
     * @return 商品栏目总数
     */
    @Select("select cid,count(1) as count from g_good where del=0 group by cid")
    @Results({
            @Result(property = "count", column = "count"),
            @Result(property = "cid", column = "cid")
    })
    List<ColumnCountVo> selectColumnGoodCount();

    /**
     * 查询商品信息
     */
    @SelectProvider(type = GoodProvider.class, method = "selectGoodInfo")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "goodId", column = "good_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "shortTitle", column = "short_title"),
            @Result(property = "desc", column = "introduce"),
            @Result(property = "pic", column = "pic"),
            @Result(property = "price", column = "price"),
            @Result(property = "commission", column = "commission"),
            @Result(property = "cid", column = "cid"),
            @Result(property = "tmall", column = "tmall"),
            @Result(property = "sellNum", column = "sell_num"),
            @Result(property = "couponId", column = "coupon_id"),
            @Result(property = "couponPrice", column = "couponprice"),
            @Result(property = "couponCondition", column = "demand"),
            @Result(property = "couponStartTime", column = "start_time"),
            @Result(property = "couponEndTime", column = "end_time"),
            @Result(property = "sellerId", column = "seller_id"),
            @Result(property = "sellerName", column = "seller_name"),
            @Result(property = "del", column = "del")

    })
    List<GoodVo> selectGoodInfo(@Param("pagekey") String pagekey, @Param("cid") int cid, @Param("type") int type, @Param("sort") String sort);

    /**
     * 保存商品
     * @param good 商品实体
     */
    @Insert("insert into g_good(good_id,title,short_title,introduce,pic,price,commission,cid,tmall,sell_num,seller_id,coupon_id,del,create_at) values(#{goodId},#{title},#{shortTitle},#{desc},#{pic},#{price},#{commission},#{cid},#{tmall},#{sellNum},#{sellerId},#{couponId},#{del},#{createTime})")
    @Options(useGeneratedKeys = true)
    void saveGood(Good good);

    /**
     * 更新商品
     * @param good 商品信息
     */
    @Update("update g_good set title=#{title},short_title=#{shortTitle},introduce=#{desc},pic=#{pic},price=#{price},commission=#{commission},cid=#{cid},tmall=#{tmall},sell_num=#{sellNum},coupon_id=#{couponId},update_at=now()  where good_id=#{goodId}")
    void updateGood(Good good);

    /**
     * 过期商品
     * @param goodId 商品ID
     */
    @Update("update g_good set del =1 where good_id=#{goodId}")
    void expireGood(@Param("goodId") String goodId);

}
